package au.com.handsunited.aop.notifications;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.model.Organisation;
import au.com.handsunited.model.invitations.Invitation;
import au.com.handsunited.service.EmailService;

/**
 * @author Arun Chandrasekhar
 *
 */
@Aspect
public class OrganisationNotificationsAdvice {

	private final EmailService emailService;


	private static Logger logger = LoggerFactory.getLogger(OrganisationNotificationsAdvice.class);


	/**
	 * @param emailService
	 */
	public OrganisationNotificationsAdvice(EmailService emailService) {
		super();
		this.emailService = emailService;
	}


	/**
	 *
	 * @param organisation
	 */
	@Pointcut("execution(* au.com.handsunited.service.impl.OrganisationServiceImpl.createOrganisation(..))")
	public void registered (final Organisation organisation) {
	    Map<String, Object> model = new HashMap<String, Object>();
	    model.put("organisation", organisation);
		emailService.sendEmail(
				organisation.getContactInfo().getEmailAddress(),
				"velocity/emails/organisation/register.vm",
				"Welcome to Hands United",
				model);
	}


	/**
	 *
	 * @param organisation
	 */
	@Pointcut("execution(* au.com.handsunited.service.impl.OrganisationServiceImpl.updateOrganisation(..))")
	public void profileUpdated (final Organisation organisation) {

	    Map<String, Object> model = new HashMap<String, Object>();
	    model.put("organisation", organisation);
		emailService.sendEmail(
				organisation.getContactInfo().getEmailAddress(),
				"velocity/emails/organisation/profileUpdated.vm",
				organisation.getName() + " has been updated",
				model);
	}


	/**
	 *
	 * @param invitation
	 */
	@Pointcut("execution(* au.com.handsunited.service.impl.OrganisationServiceImpl.inviteOrganisation(..))")
	public void invite(final Invitation invitation) {

		Map<String, Object> model = new HashMap<String, Object>();
		model.put("from", invitation.getFrom());
		model.put("organisationName", invitation.getNonMemberInviteeName());
		model.put("contactName", invitation.getNonMemberInviteeContactName());
		model.put("emailAddress", invitation.getNonMemberInviteeEmailAddress());
		model.put("message", invitation.getMessage());

		emailService.sendEmail(
				invitation.getNonMemberInviteeEmailAddress(),
				"velocity/emails/organisation/invite.vm",
				invitation.getFrom().getName() + " has invited you to join HandsUnited!",
				model);
	}


	/**
	 *
	 * @param invitationsMap
	 */
	@Pointcut("execution(* au.com.handsunited.service.impl.OrganisationServiceImpl.inviteToOrganisation(..))")
	public void inviteTo(Map<String, List<Invitation>> invitationsMap) {

		List<Invitation> nonMemberInvitations = invitationsMap.get(Invitation.VALID_EMAILADDRESSES);
		List<Invitation> memberInvitations = invitationsMap.get(Invitation.INVITED_ENTITIES);

		for (Invitation invitation : memberInvitations) {

			Map<String, Object> model = new HashMap<String, Object>();
			model.put("from", invitation.getFrom());
			model.put("organisation", invitation.getTo());
			model.put("message", invitation.getMessage());

			emailService.sendEmail(
					invitation.getInvitee().getContactInfo().getEmailAddress(),
					"velocity/emails/organisation/inviteto.vm",
					invitation.getFrom().getName() + " has invited you to join " + invitation.getTo().getName(),
					model);
		}

		for (Invitation invitation : nonMemberInvitations) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("from", invitation.getFrom());
			model.put("organisation", invitation.getTo());
			model.put("message", invitation.getMessage());

			emailService.sendEmail(
					invitation.getNonMemberInviteeEmailAddress(),
					"velocity/emails/organisation/inviteto.vm",
					invitation.getFrom().getName() + " has invited you to join " + invitation.getTo().getName(),
					model);
		}

	}


}
